/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * bbb.java
 *
 * Created on 23-oct-2011, 13:52:38
 */
package vistaDibujable;

import controladorjuegolp2.pCivilizationG;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author hp
 */
public class ventanaJuego extends javax.swing.JApplet {

    /** Initializes the applet bbb */
    Image bufferI,imageMap;
    Graphics g;
    pCivilizationG controladorJ;
    boolean estaJugando=false;
    
    @Override
    public void init() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ventanaJuego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ventanaJuego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ventanaJuego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ventanaJuego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the applet */
        //System.out.println((new File("imagenes\\c0mapa.png")).getAbsolutePath());
        try{
         bufferI= ImageIO.read(new File(".\\imagenes\\fondoj.png"));
         imageMap=ImageIO.read(new File(".\\imagenes\\c0mapa.png"));
         //bufferI= ImageIO.read(new URL("http://bibooz.com/humor/conejo.jpg"));
          //  bufferI= getImage(getCodeBase(),".\\imagenes\\fondoj.png").;
            System.out.println("hola: " +getCodeBase());
        } catch (Exception ex){
            Logger.getLogger(ventanaJuego.class.getName()).log(Level.SEVERE, null, ex);
        }
        db = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);


   
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    
    
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated bay the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                formFocusLost(evt);
            }
        });
        addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                formPropertyChange(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem1.setText("Abrir juego");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Nueva partida");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 540, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 459, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would open the file.
          //  log.append("Opening: " + file.getName() + "." + newline);
        } else {
            //log.append("Open command cancelled by user." + newline);
        }
        this.repaint();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        String nombreJ = JOptionPane.showInputDialog("ingresa nombre del jugador");
        if (!nombreJ.isEmpty())
        {
          estaJugando=true;
          jMenuBar1.setVisible(false);
          this.repaint();
            try {
                controladorJ = new pCivilizationG();
                controladorJ.cargarXMLInicial("amigos");
            } catch (Exception ex) {
            Logger.getLogger(ventanaJuego.class.getName()).log(Level.SEVERE, null, ex);
           
        }
        }

        //controladorJ.jugar();
        //System.out.println(res);
       // this.repaint();
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void formFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusLost
       this.repaint(); // TODO add your handling code here:
    }//GEN-LAST:event_formFocusLost

    private void formPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_formPropertyChange
       this.repaint(); // TODO add your handling code here:
    }//GEN-LAST:event_formPropertyChange

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    // End of variables declaration//GEN-END:variables


    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
    }

 private ArrayList<String> inicializaTexto(Graphics g)
{
	 
	ArrayList<String> bufftext= new ArrayList<String>();
	bufftext.add("                   !!!!!!!!!!!!!!!!!Peru Civilization 2011!!!!!!!!!!!!!!                 ");
		bufftext.add("Construye tu ciudad con casas, edificios e industrias productoras de recursos para tu ciudad ");
		bufftext.add("No olvides que tenemos que defendernos del enemigo, y construye tus edificios militares ");
		bufftext.add("Las instrucciones del juego son fáciles, administra tu ciudad y Defiendela del enemigo ");
         

	return bufftext;
 }
 
 public void pintaJuegoInicial(Graphics g2d,Image I)
 {
     int x=60,y=200,z=25;
     if(I!=null)
            g2d.drawImage(I, 0, 0, this);
            else
            g2d.drawString("No encuentra imagen",100, 100);        

            
		ArrayList<String> textGame = new ArrayList<String>();
		textGame=inicializaTexto(g2d);
		for(int j=0;j<textGame.size();j++)
		{
			if (j==0){
                    g2d.setFont(new Font("arial",Font.BOLD,20));

                }
			if (j==1)
				{
                    g2d.setFont(new Font("arial",Font.BOLD,17));
                }
                    g2d.drawString(textGame.get(j), x, y);
                    y+=z;
		}
                this.jMenuBar1.repaint();
 }

 public void pintaJuegoMapa(Graphics g2d,Image I)
 {

     if(I!=null)
            g2d.drawImage(I, 0, 0, this);
            else
            g2d.drawString("No encuentra imagen",100, 100);
 }
	  

 @Override
    public void paint(Graphics g){
	
		super.paint(g);
               db = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
        Graphics g2d = (Graphics2D)db.getGraphics();
      
        if(!estaJugando)
        {
            pintaJuegoInicial(g2d,bufferI);

        }
        else
        {
            pintaJuegoMapa(g2d,imageMap);
        }


        g.drawImage(db,0, 0, this);
        

    }

    static int i;
    BufferedImage db;


    


            
    
    
}
